/**
 * Created on [2022-11-11] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.common.exception.ServiceException;
import com.sunnada.btfvilg.entity.Deliveryman;
import com.sunnada.btfvilg.mapper.DeliverymanMapper;
import com.sunnada.btfvilg.model.request.DelimanParamVo;
import com.sunnada.btfvilg.model.request.UserParamVo;
import com.sunnada.btfvilg.model.response.DelimanListVo;
import com.sunnada.btfvilg.model.response.UserVo;
import com.sunnada.btfvilg.service.DeliverymanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class DeliverymanServiceImpl extends ServiceImpl<DeliverymanMapper, Deliveryman> implements DeliverymanService {


    @Autowired(required = false)
    private DeliverymanMapper deliverymanMapper;

    @Override
    public UserVo login(UserParamVo deliManVo) {
        QueryWrapper<Deliveryman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dname", deliManVo.getUsername());
        Deliveryman deliMan = getOne(queryWrapper);
        if (deliMan == null) {
            throw new ServiceException("用户不存在");
        }
        String inputPassword = deliManVo.getPassword();
        if (Objects.equals(inputPassword, deliMan.getDpassword())) {
            return new UserVo(deliMan.getDid(),deliMan.getDname());
        } else {
            throw new ServiceException("用户名或密码错误");
        }
    }

    @Override
    public DelimanListVo getDelimanList(String sid, Integer page, Integer numPerPage) {
        DelimanListVo delimanList = new DelimanListVo();
        QueryWrapper<Deliveryman> qw = new QueryWrapper<>();
        Integer count = 0;
        List<Deliveryman> delimans = null;
        if(StringUtils.isEmpty(sid)){
            delimans = deliverymanMapper.getDelimanListAll((page - 1) * numPerPage, numPerPage);
        }else{
            qw.eq("sid",sid);
            delimans = deliverymanMapper.getDelimanList(sid, (page - 1) * numPerPage, numPerPage);
        }
        count = deliverymanMapper.selectCount(qw);
        delimanList.setTotal(count);
        delimanList.setDelimanList(delimans);
        delimanList.setPage(page);
        delimanList.setNumPerPage(numPerPage);
        return delimanList;
    }

    @Override
    public UserVo addDeliman(DelimanParamVo delimanVo) {
        QueryWrapper<Deliveryman> qw = new QueryWrapper<>();
        qw.eq("dname", delimanVo.getDname());
        Integer count = deliverymanMapper.selectCount(qw);
        if(count != 0 ){
            throw new ServiceException("名称已存在，请调整");
        }
        Deliveryman deliman = new Deliveryman();
        deliman.setDid(UUID.randomUUID().toString().replaceAll("-",""));
        deliman.setSid(delimanVo.getSid());
        deliman.setDname(delimanVo.getDname());
        deliman.setDpassword(delimanVo.getDpassword());
        deliman.setDphone(delimanVo.getDphone());
        deliman.setDstate("在岗");
        deliverymanMapper.insert(deliman);
        UserVo reg = new UserVo(deliman.getDid(),deliman.getDname());
        return reg;
    }

    @Override
    public void delDeliman(String did) {
        deliverymanMapper.deleteById(did);
    }

    @Override
    public void updateDeliman(DelimanParamVo delimanVo) {
        Deliveryman deliman = new Deliveryman();
        deliman.setDid(delimanVo.getDid());
        deliman.setSid(delimanVo.getSid());
        deliman.setDname(delimanVo.getDname());
        deliman.setDpassword(delimanVo.getDpassword());
        deliman.setDphone(delimanVo.getDphone());
        deliman.setDstate(delimanVo.getDstate());
        deliverymanMapper.updateById(deliman);
    }

    @Override
    public Deliveryman getDelimanById(String did) {
        return deliverymanMapper.selectById(did);
    }
}
